//index
(function(){
	$.post(
		basepath+"/user/getUser",
   	     "",
   	     function(data){
   	    	 if(data=='0'||data=='1'||data=='2'){
   	    		 $("#userLogOrWelcome").empty().append("<a role='button' data-toggle='modal' data-target='#login-modal'>登陆/注册</a>");
   	    	 }else{
   	    		 $("#userLogOrWelcome").empty().append("<a class='dropdown-toggle' data-toggle='dropdown' role='button' aria-haspopup='true' aria-expanded='false'>"+data+"<span class='caret'></span></a> "+
	    				 "<ul class='dropdown-menu'> " +
	    				 "<li><a target='_blank' href='"+basepath+"/personal/collection.jsp'>个人主页</a></li> "+
	    				 "<li><a href='javascript:logout()'>退出登录</a></li> "+
	    				 "</ul> ");
   	    	 }
   	    }
    );
}());
var accountIsOk = false;
function login(){
	$.post( 	
		 basepath+"/user/login",
   	     {"mobile":$("#login_username").val(),"psswrd":$("#login_password").val()},
   	     function(data){
   	    	 if(data=='1'||data=='2'){
   	    		alert("账号或密码不正确！");
   	    		return;
   	    	 }else{
   	    		 $("#userLogOrWelcome").empty().append("<a class='dropdown-toggle' data-toggle='dropdown' role='button' aria-haspopup='true' aria-expanded='false'>"+data+"<span class='caret'></span></a> "+
   	    				 "<ul class='dropdown-menu'> " +
   	    				 "<li><a target='_blank' href='"+basepath+"/personal/collection.jsp'>个人主页</a></li> "+
   	    				 "<li><a href='javascript:logout()'>退出登录</a></li> "+
   	    				 "</ul> ");
   	    		$("#login-modal").css("display","none");
   	    	 }
   	    }
    );
}

function register(){
	if(!accountIsOk) return;
	var name = $("#register_username").val();
	var phone = $("#register_phone").val();
	var psw1 = $("#register_password1").val();
	var psw2 = $("#register_password2").val();
	if(psw1!=psw2){
		alert("两次输入的密码不一致！");
		return;
	}
	if(!name || !phone) return;
	$.post( 	
		 basepath+"/user/register",
   	     {"name":name,"mobile":phone,"psswrd":psw1},
   	     function(data){ 
   	    	 if(data=='1'||data=='2'){
   	    		 alert("字符串不能为空！");
   	    		 return;
   	    	 }else{    		 
   	    		alert("注册成功");
   	    		window.location.reload();
   	    		$("#login-modal").css("display","none");
   	    	 }
   	    }
    );
}

function logout(){
	$.post(
		 basepath+"/user/logout",
   	     "",
   	     function(data){
   	    	window.location.reload();
   	    }
    );
}

function checkExist(){
	accountIsOk = false;
	$.post( 	
			     basepath+"/user/isPhoneExisted",
	    	     {"mobile":$("#register_phone").val()},
	    	     function(data){
	    	    	 if(data == 0){
	    	    		 $("#regisPhoMsg").empty().append("Okay");
	    	    		 accountIsOk = true;
	    	    	 }else if(data == 1){		
	    	    		 $("#regisPhoMsg").empty().append("已被注册");
	    	    	 }else{
	    	    		$("#regisPhoMsg").empty().append("无效账户");
	    	    	 }
	    	    }
	    );
}